#!/bin/bash
#
# Test the PHP image in OpenShift.
#
# IMAGE_NAME specifies a name of the candidate image used for testing.
# The image has to be available before this script is executed.
#

THISDIR=$(dirname ${BASH_SOURCE[0]})

source ${THISDIR}/test-lib.sh
source ${THISDIR}/test-lib-openshift.sh
source ${THISDIR}/test-lib-php.sh

# change the branch to a different value if a new change in the example
# app needs to be tested
BRANCH_TO_TEST=master

set -eo nounset

trap ct_os_cleanup EXIT SIGINT

ct_os_check_compulsory_vars

ct_os_enable_print_logs

istag="php:$VERSION"

function test_file_upload() {
  local image_name=$1
  local service_name=${image_name##*/}
  local app="https://github.com/openshift-qe/openshift-php-upload-demo"
  local ip=""

  echo "Running file upload test for: $image_name"
  ct_os_new_project
  ct_os_upload_image "$image_name" "$istag"

  ct_os_deploy_s2i_image "$istag" "$app" --name "${service_name}"
  ct_os_wait_pod_ready "$service_name" 60

  # Wait until the app is prepared to receive files
  ip=$(ct_os_get_service_ip "$service_name")
  curl "$ip:8080" 2>/dev/null | grep -q "OpenShift File Upload Demonstration"

  # Upload a file into the pod using the php app
  curl -F fto=@README.md "$ip:8080/upload.php" &>/dev/null
  ct_os_run_in_pod "$(ct_os_get_pod_name "$service_name")" ls uploaded/README.md >/dev/null

  ct_os_delete_project
}

test_latest_imagestreams() {
  local result=1
  # Switch to root directory of a container
  pushd "${test_dir}/../.." >/dev/null || return 1
  ct_check_latest_imagestreams
  result=$?
  popd >/dev/null || return 1
  return $result
}

ct_os_cluster_up

# test local app
ct_os_test_s2i_app ${IMAGE_NAME} "https://github.com/sclorg/s2i-php-container.git" ${VERSION}/test/test-app "Test PHP passed"

ct_os_test_s2i_app ${IMAGE_NAME} "https://github.com/sclorg/cakephp-ex.git#${BRANCH_TO_TEST}" . 'Welcome to your CakePHP application on OpenShift'

# cakephp template does not work with version 5.6
if [[ "${VERSION}" > "5.6" ]] ; then
  ct_os_test_template_app ${IMAGE_NAME} \
                        https://raw.githubusercontent.com/sclorg/cakephp-ex/${BRANCH_TO_TEST}/openshift/templates/cakephp.json \
                        php \
                        'Welcome to your CakePHP application on OpenShift' \
                        8080 http 200 "-p SOURCE_REPOSITORY_REF=${BRANCH_TO_TEST} -p SOURCE_REPOSITORY_URL=https://github.com/sclorg/cakephp-ex.git -p PHP_VERSION=${VERSION} -p NAME=php-testing"
fi

# test image update with s2i
old_image=$(ct_get_public_image_name "$OS" "$BASE_IMAGE_NAME" "$VERSION")

if ct_check_image_availability "$old_image"; then
  ct_os_test_image_update "$IMAGE_NAME" "${old_image}" "$istag" \
                          'ct_test_response "http://<IP>:8080" "200" "Test PHP passed"' \
                          "$istag~https://github.com/sclorg/s2i-php-container.git" \
                          --context-dir="$VERSION/test/test-app"
else
  echo "Image $old_image not found in public repositories, skipping update test."
fi

test_file_upload "$IMAGE_NAME"

test_latest_imagestreams

# Check the imagestream
test_php_imagestream

OS_TESTSUITE_RESULT=0

ct_os_cluster_down

# vim: set tabstop=2:shiftwidth=2:expandtab:

